package cn.snow.iot.module.warning.service.action;


import cn.snow.iot.framework.common.pojo.CommonResult;
import cn.snow.iot.module.warning.controller.admin.action.vo.WarningActionPageVO;
import cn.snow.iot.module.warning.controller.admin.action.vo.WarningActionSaveOrUpdateVO;
import cn.snow.iot.module.warning.controller.admin.home.vo.ActionTypeCountVO;
import cn.snow.iot.module.warning.dal.dataobject.action.WarningActionDO;
import cn.snow.iot.module.warning.dal.mysql.action.WarningActionMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WarningActionServiceImpl extends ServiceImpl<WarningActionMapper, WarningActionDO> implements IWarningActionService {


    @Override
    public CommonResult saveUpdate(WarningActionSaveOrUpdateVO action) {
        WarningActionDO warningActionDO = new WarningActionDO();
        BeanUtils.copyProperties(action, warningActionDO);
        saveOrUpdate(warningActionDO);
        return CommonResult.success(warningActionDO);
    }

    @Override
    public IPage<WarningActionDO> selectWarningPage(WarningActionPageVO actionPage) {
        IPage<WarningActionDO> page = new Page<>(actionPage.getPageNo(), actionPage.getPageSize());
        return baseMapper.selectActionPage(page, actionPage);
    }

    @Override
    public Boolean removeAction(List<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    @Override
    public CommonResult getActionById(Long id) {
        // 获取动作原始数据
        WarningActionDO actionDO = getById(id);
        WarningActionSaveOrUpdateVO warningAction = new WarningActionSaveOrUpdateVO();
        BeanUtils.copyProperties(actionDO, warningAction);
        if (StringUtils.isNotEmpty(actionDO.getRelatedParameter())){
            JSONObject jsonObject = JSONObject.parseObject(actionDO.getRelatedParameter());
            warningAction.setMessage(jsonObject);
        }
        return CommonResult.success(warningAction);
    }

    @Override
    public List<ActionTypeCountVO> getActionTypeCount() {
        return baseMapper.getActionTypeCount();
    }
}
